package cn.krguang;

import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {

    public static void main(String[] args) {
        DbOperate dbOperate = DbOperate.getInstance();
        MyMqttClient myMqttClient = MyMqttClient.getInstance();
        dbOperate.creatList();

        myMqttClient.setConnect();
        myMqttClient.setOnServerReadStringCallback(new MyMqttClient.OnServerReadStringCallback() {
            @Override
            public void callback(String topic, MqttMessage message) {

              //  System.out.println("message = "+message);
                String[] strArray = topic.split("/");
                String imei = strArray[1];
                if (imei.length() > 5){ //imei长度大于5才处理，过滤掉气体部分
                    try {
                        JSONObject jsonObject = new JSONObject(message.toString());

                        String status = jsonObject.has("status") ? jsonObject.getString("status") : "";
                        String dataType = jsonObject.has("dataType") ? jsonObject.getString("dataType") : "";
                        if (status.equals("online")){

                            String unitName = jsonObject.has("unitName") ? jsonObject.getString("unitName") : "0";
                            String tempReal = jsonObject.has("tempReal") ? jsonObject.getString("tempReal") : "0";
                            String humiReal = jsonObject.has("humiReal") ? jsonObject.getString("humiReal") : "0";
                            String tempSet = jsonObject.has("tempSet") ? jsonObject.getString("tempSet") : "0";
                            String humiSet = jsonObject.has("humiSet") ? jsonObject.getString("humiSet") : "0";
                            String yaCha1 = jsonObject.has("yaCha1") ? jsonObject.getString("yaCha1") : "0";

                            String lengShuiFaKaiDu = jsonObject.has("lengShuiFaKaiDu") ? jsonObject.getString("lengShuiFaKaiDu") : "0";
                            String reShuiFaKaiDu = jsonObject.has("reShuiFaKaiDu") ? jsonObject.getString("reShuiFaKaiDu") : "0";
                            String jiaShiQIKaiDu = jsonObject.has("jiaShiQIKaiDu") ? jsonObject.getString("jiaShiQIKaiDu") : "0";
                            String dongXiaJi = jsonObject.has("dongXiaJi") ? jsonObject.getString("dongXiaJi") : "0";
                            String shouZiDong = jsonObject.has("shouZiDong") ? jsonObject.getString("shouZiDong") : "0";
                            String fengJiYiQiDong = jsonObject.has("fengJiYiQiDong") ? jsonObject.getString("fengJiYiQiDong") : "0";
                            String zhiBanStatus = jsonObject.has("zhiBanStatus") ? jsonObject.getString("zhiBanStatus") : "0";

                            String dianJiaRe1 = jsonObject.has("dianJiaRe1") ? jsonObject.getString("dianJiaRe1") : "0";
                            String dianJiaRe2 = jsonObject.has("dianJiaRe2") ? jsonObject.getString("dianJiaRe2") : "0";
                            String dianJiaRe3 = jsonObject.has("dianJiaRe3") ? jsonObject.getString("dianJiaRe3") : "0";
                            String fengJiStatus = jsonObject.has("fengJiStatus") ? jsonObject.getString("fengJiStatus") : "";
                            String zhongXiaoBaoJing = jsonObject.has("zhongXiaoBaoJing") ? jsonObject.getString("zhongXiaoBaoJing") : "0";
                            String dianJiaReGaoWen = jsonObject.has("dianJiaReGaoWen") ? jsonObject.getString("dianJiaReGaoWen") : "0";
                            String fengJiQueFeng = jsonObject.has("fengJiQueFeng") ? jsonObject.getString("fengJiQueFeng") : "0";
                            String paiFengJiYiQiDong = jsonObject.has("paiFengJiYiQiDong") ? jsonObject.getString("paiFengJiYiQiDong") : "0";
                            String diWenPanGuan = jsonObject.has("diWenPanGuan") ? jsonObject.getString("diWenPanGuan") : "0";
                            String mieJunYunXing = jsonObject.has("mieJunYunXing") ? jsonObject.getString("mieJunYunXing") : "0";

                            String xinFengWenDu = jsonObject.has("xinFengWenDU") ? jsonObject.getString("xinFengWenDU") : "0";
                            String dianYuRe1 = jsonObject.has("dianYuRe1") ? jsonObject.getString("dianYuRe1") : "0";
                            String dianYuRe2 = jsonObject.has("dianYuRe2") ? jsonObject.getString("dianYuRe2") : "0";
                            String dianYuRe3 = jsonObject.has("dianYuRe3") ? jsonObject.getString("dianYuRe3") : "0";
                            String dianYuReGaoWen = jsonObject.has("dianYuReGaoWen") ? jsonObject.getString("dianYuReGaoWen") : "0";

                            if (tempReal.equals("")) tempReal="0";
                            if (humiReal.equals("")) humiReal="0";
                            if (tempSet.equals("")) tempSet="0";
                            if (humiSet.equals("")) humiSet="0";
                            if (yaCha1.equals("")) yaCha1="0";
                            if (lengShuiFaKaiDu.equals("")) lengShuiFaKaiDu="0";
                            if (reShuiFaKaiDu.equals("")) reShuiFaKaiDu="0";
                            if (jiaShiQIKaiDu.equals("")) jiaShiQIKaiDu="0";
                            if (dongXiaJi.equals("")) dongXiaJi="0";
                            if (shouZiDong.equals("")) shouZiDong="0";
                            if (fengJiYiQiDong.equals("")) fengJiYiQiDong="0";
                            if (zhiBanStatus.equals("")) zhiBanStatus="0";
                            if (dianJiaRe1.equals("")) dianJiaRe1="0";
                            if (dianJiaRe2.equals("")) dianJiaRe2="0";
                            if (dianJiaRe3.equals("")) dianJiaRe3="0";
                            if (fengJiStatus.equals("")) fengJiStatus="0";
                            if (zhongXiaoBaoJing.equals("")) zhongXiaoBaoJing="0";
                            if (dianJiaReGaoWen.equals("")) dianJiaReGaoWen="0";
                            if (fengJiQueFeng.equals("")) fengJiQueFeng="0";
                            if (paiFengJiYiQiDong.equals("")) paiFengJiYiQiDong="0";
                            if (diWenPanGuan.equals("")) diWenPanGuan="0";
                            if (mieJunYunXing.equals("")) mieJunYunXing="0";
                            if (xinFengWenDu.equals("")) xinFengWenDu="0";
                            if (dianYuRe1.equals("")) dianYuRe1="0";
                            if (dianYuRe2.equals("")) dianYuRe2="0";
                            if (dianYuRe3.equals("")) dianYuRe3="0";
                            if (dianYuReGaoWen.equals("")) dianYuReGaoWen="0";

                            UnitData unitData = new UnitData();
                            unitData.setImei(imei);
                            unitData.setUnitName(unitName);
                            unitData.setTempReal(tempReal);
                            unitData.setHumiReal(humiReal);
                            unitData.setTempSet(tempSet);
                            unitData.setHumiSet(humiSet);
                            unitData.setYaCha1(yaCha1);
                            unitData.setLengShuiFaKaiDu(lengShuiFaKaiDu);
                            unitData.setReShuiFaKaiDu(reShuiFaKaiDu);
                            unitData.setJiaShiQIKaiDu(jiaShiQIKaiDu);
                            unitData.setDongXiaJi(dongXiaJi);
                            unitData.setShouZiDong(shouZiDong);
                            unitData.setFengJiYiQiDong(fengJiYiQiDong);
                            unitData.setZhiBanStatus(zhiBanStatus);
                            unitData.setDianJiaRe1(dianJiaRe1);
                            unitData.setDianJiaRe2(dianJiaRe2);
                            unitData.setDianJiaRe3(dianJiaRe3);
                            unitData.setFengJiStatus(fengJiStatus);
                            unitData.setZhongXiaoBaoJing(zhongXiaoBaoJing);
                            unitData.setDianJiaReGaoWen(dianJiaReGaoWen);
                            unitData.setFengJiQueFeng(fengJiQueFeng);
                            unitData.setPaiFengJiYiQiDong(paiFengJiYiQiDong);
                            unitData.setDiWenPanGuan(diWenPanGuan);
                            unitData.setMieJunYunXing(mieJunYunXing);
                            unitData.setXinFengWenDu(xinFengWenDu);
                            unitData.setDianYuRe1(dianYuRe1);
                            unitData.setDianYuRe2(dianYuRe2);
                            unitData.setDianYuRe3(dianYuRe3);
                            unitData.setDianYuReGaoWen(dianYuReGaoWen);

                            dbOperate.insertUnitData(unitData);
                        }

                        if (dataType.equals("warning")){

                            String warningName = "";
                            String warningData = "";
                            String unitName = jsonObject.has("unitName") ? jsonObject.getString("unitName") : "0";

                            if (jsonObject.has("dianJiaReGaoWen")){
                                warningName = "dianJiaReGaoWen";
                                warningData = jsonObject.getString("dianJiaReGaoWen");
                            }else if (jsonObject.has("gaoXiaoBaoJing")){
                                warningName = "gaoXiaoBaoJing";
                                warningData = jsonObject.getString("gaoXiaoBaoJing");
                            }else if (jsonObject.has("diWenPanGuan")){
                                warningName = "diWenPanGuan";
                                warningData = jsonObject.getString("diWenPanGuan");
                            }else if (jsonObject.has("fengJiQueFeng")){
                                warningName = "fengJiQueFeng";
                                warningData = jsonObject.getString("fengJiQueFeng");
                            }else if (jsonObject.has("dianYuReGaoWen")){
                                warningName = "dianYuReGaoWen";
                                warningData = jsonObject.getString("dianYuReGaoWen");
                            }else if (jsonObject.has("zhongXiaoBaoJing")){
                                warningName = "zhongXiaoBaoJing";
                                warningData = jsonObject.getString("zhongXiaoBaoJing");
                            }else if (jsonObject.has("jiTingBaoJing")){
                                warningName = "jiTingBaoJing";
                                warningData = jsonObject.getString("jiTingBaoJing");
                            }else if (jsonObject.has("chuXiaoBaoJing")){
                                warningName = "chuXiaoBaoJing";
                                warningData = jsonObject.getString("chuXiaoBaoJing");
                            }

                            WarningData mWarningData = new WarningData();
                            mWarningData.setUnitName(unitName);
                            mWarningData.setWarningData(warningData);
                            mWarningData.setWarningName(warningName);
                            mWarningData.setImei(imei);
                            dbOperate.insertWarningData(mWarningData);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}
